package com.akeil.flash.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.akeil.flash.dao.DataMapper;
import com.akeil.flash.pojo.Data;
import com.akeil.flash.pojo.DataExample;

@Service
public class DataServiceImpl implements IDataService {
	@Autowired
	DataMapper dataMapper;
	
	@Override
	public void insert(Data data) {
		// TODO Auto-generated method stub
		dataMapper.insertSelective(data);
	}

	@Override
	public void deleteByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		dataMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Data> selectAll() {
		// TODO Auto-generated method stub
		return dataMapper.selectByExample(null);
	}

	@Override
	@Test
	public List<String> selectDistinctTestDate() {
		// TODO Auto-generated method stub
		/*List<String> lists = dataMapper.selectDistinctTestDate();
		return lists;*/
		return null;
	}

	@Override
	public int deleteByDateFromTo(String from, String to) {
		// TODO 自动生成的方法存根
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andTesttimeBetween(from, to);
		
		return dataMapper.deleteByExample(dataExample);
	}

	@Override
	public List<Data> selectTestNumberByTwoTestDate(String testdateFrom,
			String testdateTo) {
		// TODO 自动生成的方法存根
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andTesttimeBetween(testdateFrom, testdateTo);
		
		return dataMapper.selectByExample(dataExample);
	}

	@Override
	public List<String> selectSamplenameByTestDate(String testdate) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public List<String> selectTestYear() {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public List<String> selectTestMonthByYear(String year) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public List<String> selectTestDayByYearMonth(String yearmonth) {
		// TODO 自动生成的方法存根
		return null;
	}

	@Override
	public List<Data> selectDataBySamplename(String samplename) {
		// TODO 自动生成的方法存根
		DataExample example = new DataExample();
		DataExample.Criteria criteria = example.createCriteria();
		criteria.andSamplenameEqualTo(samplename);
		
		return dataMapper.selectByExample(example);
	}

	@Override
	public List<Data> selectDataByIds(List<Integer> ids) {
		// TODO 自动生成的方法存根
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andIdIn(ids);
		
		return dataMapper.selectByExample(dataExample);
	}

	@Override
	public Data selectDataByPrimaryKey(Integer id) {
		return dataMapper.selectByPrimaryKey(id);
	}

	
	@Override
	public int updateByPrimayKey(Data data) {
		// TODO 自动生成的方法存根
		return dataMapper.updateByPrimaryKey(data);
	}

	
	@Override
	public List<Data> selectDataToday() {
		// TODO 自动生成的方法存根
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String strToday = df.format(new Date());
		
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andTesttimeLike("%" + strToday + "%");
		
		dataExample.setOrderByClause("testtime desc");
		
		return dataMapper.selectByExample(dataExample);
	}

	@Override
	public List<Data> selectDatasByTestDate(String testdate) {
		// TODO 自动生成的方法存根
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andTesttimeLike("%" + testdate + "%");
		
		return dataMapper.selectByExample(dataExample);
	}

	@Override
	public List<Data> selectDataByPeriodAndName(String testdateFrom,
			String testdateTo, String samplename,String sampleno) {
		// TODO 自动生成的方法存根
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		if(samplename != null){
			criteria.andSamplenameLike("%" + samplename + "%");
		}
		
		if(testdateFrom != null && testdateTo != null){
			criteria.andTesttimeBetween(testdateFrom, testdateTo);
		}
		
		if(sampleno != null){
			criteria.andSamplenoLike("%" + sampleno + "%");
		}
		
		dataExample.setOrderByClause("testtime desc");
		
		return dataMapper.selectByExample(dataExample);
	}

	/* (非 Javadoc) 
	* <p>Title: selectDataToday</p> 
	* <p>Description: </p> 
	* @param status
	* @return 
	* @see com.akeil.sulfur.service.IDataService#selectDataToday(int) 
	*/
	
	@Override
	public List<Data> selectDataTodayNoStatus(int status) {
		// TODO 自动生成的方法存根
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String strToday = df.format(new Date());
		
		DataExample dataExample = new DataExample();
		DataExample.Criteria criteria = dataExample.createCriteria();
		criteria.andTesttimeLike("%" + strToday + "%");
		
		criteria.andStatusNotEqualTo(status);
		
		dataExample.setOrderByClause("testtime desc");
		
		return dataMapper.selectByExample(dataExample);
	}

}
